﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
using ImageManipulation;

namespace Cms.Web.Tools
{
    /// <summary>
    /// MergeImg 的摘要说明
    /// </summary>
    public class MergeImg : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "image/PNG";
            MergeImage(context);
        }
        public void MergeImage(HttpContext context)
        {
            int size = 16;
            string imgPath = context.Server.MapPath(String.Format("~/Images/Icon/{0}", size));
            if (System.IO.Directory.Exists(imgPath))
            {
                System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(imgPath);
                System.IO.FileInfo[] fis = di.GetFiles();
                Size ImgSize = new Size(size, size * fis.Length);
                Color bgColor = Color.Transparent;
                using (Bitmap bigImg = new Bitmap(ImgSize.Width, ImgSize.Height))
                {
                    string imgType = "png";
                    ImageFormat format = ImageFormat.Png;
                    switch (imgType)
                    {
                        case "jpeg":
                            format = ImageFormat.Jpeg;
                            break;
                        case "jpg":
                            format = ImageFormat.Jpeg;
                            break;
                        case "gif":
                            format = ImageFormat.Gif;
                            break;
                        default:
                            break;
                    }
                    using (Graphics g = Graphics.FromImage(bigImg))
                    {
                        //设置高质量插值法 
                        g.InterpolationMode = InterpolationMode.High;
                        //设置高质量,低速度呈现平滑度 
                        g.SmoothingMode = SmoothingMode.HighQuality;
                        //清空画布并以透明背景色填充 
                        if (bgColor == Color.Transparent && (format == ImageFormat.Jpeg || format == ImageFormat.Gif)) g.Clear(Color.White);
                        else g.Clear(bgColor);
                        //foreach (PictureBox pb in panelImages.Controls)
                        int RectX = 0;
                        int RectY = 0;
                        foreach(FileInfo fi in fis )
                        {
                            string imgName = Path.GetFileNameWithoutExtension(fi.Name);
                            Image img = Image.FromFile(fi.FullName);
                            g.DrawImage(img, RectX, RectY, img.Size.Width, img.Size.Height);
                            RectY = RectY + img.Size.Height;
                        }
                    }

                    using (MemoryStream ImgStream = new MemoryStream())
                    {
                        if (format == ImageFormat.Gif)
                        {
                            OctreeQuantizer quantizer = new OctreeQuantizer(255, 8);

                            using (Bitmap quantized = quantizer.Quantize(bigImg))
                            {
                                //quantized.Save(imgPath, format);
                                quantized.Save(ImgStream, format);
                            }
                        }
                        else
                        {
                            //bigImg.Save(imgPath, format);
                            bigImg.Save(ImgStream, format);
                        }
                        context.Response.BinaryWrite(ImgStream.ToArray());
                        context.Response.End();
                    }
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}